<?php

/**
 * uniBoard Board Main Page
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_index{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Action Templates
	 *
	 * @var object
	 */
	
	private $templates		= NULL;
	
	/**
	 * Template Vars
	 *
	 * @var arrat
	 */
	
	private $tpl_vars		= array();
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Load Index Page
		$appCore -> loadLanguageFile('board_index');
				
		// Load Index Templates
		$this -> templates = new tpl_board_index( $appCore);
		
		// Set Some basic stats
		$this -> tpl_vars['stat_users'] = $appCore -> conf['stat_users'];
		$this -> tpl_vars['stat_posts'] = $appCore -> conf['stat_posts'];
		$this -> tpl_vars['stat_topics'] = $appCore -> conf['stat_topics'];
		$this -> tpl_vars['last_user'] = $appCore -> conf['stat_last_user'];
		$this -> tpl_vars['last_user_name'] = '<a href="' . $appCore -> niceLink( 'act=user&user=' . $appCore -> conf['stat_last_user'], $appCore -> conf['stat_last_user_name']) . '">' . htmlspecialchars( $appCore -> conf['stat_last_user_name']) . '</a>';
		
		// Start Forums List
		$forums_list = new action_forums_list($appCore, 0);
		
		// Draw It
		$this -> tpl_vars['forums_list'] = $forums_list -> draw();
		
		// Garbage out
		unset( $forums_list);
		
		// Draw Sessions?
		if ($appCore -> conf['online_list_index'])
		{
			// Show Onlines
			$this -> tpl_vars['show_online'] = 1;
			
			// Show Detailed list?
			$this -> tpl_vars['show_online_details'] = $appCore -> conf['sessions_allow_browse'];
			
			// Run Action
			$this -> drawOnline();
		}
		
		// Draw Last online?
		if ($appCore -> conf['last_list_index'] > 0)
		{
			// Show Onlines
			$this -> tpl_vars['show_last_active'] = 1;
			
			// Run Action
			$this -> drawLastActive();
		}
		
		// Draw output
		$appCore -> output -> draw( '', array(), $this -> templates -> tpl_page_layout( $this -> tpl_vars));
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws On-Line users
	 *
	 */
	
	function drawOnline(){
		
		// Select Groups for legend
		$user_groups = class_cache::loadCache( 'user_groups');
		
		// Not Found?
		if ( class_cache::$load_error)
		{
			// Empty array
			$user_groups = array();
			
			// Select Them
			$this -> appCore -> db -> query( 'SELECT group_id, group_name, group_prefix, group_suffix, group_hidden
			FROM ' . DB_PREFIX . 'user_groups
			ORDER BY group_pos');
			
			// Pass them to array
			while ( $group_result = $this -> appCore -> db -> fetch_assoc())
			{
				$user_groups[$group_result['group_id']] = $group_result;
			}
			
			// Free Result
			$this -> appCore -> db -> freeResult();
			
			// Save Cache
			class_cache::saveCache( 'user_groups', $user_groups);
		}
		
		// Sort Off Bad groups
		foreach ( $user_groups as $group_id => $group_data)
		{
			// Display Group?
			if ( $group_data['group_hidden'])
			{
				// Remove Group
				unset( $user_groups[$group_id]);
			}
			else
			{
				// Set Group name
				$user_groups[$group_id] = array( 0, $group_data['group_prefix'] . $this -> appCore -> ifString( htmlspecialchars( $group_data['group_name'])) . $group_data['group_suffix']);
			}
		}
		
		// Select Sessions
		$this -> appCore -> db -> query( 'SELECT u.user_id, u.user_name, u.group_id, s.session_hidden, c.crawler_id, c.crawler_name,
		ug.group_prefix AS user_prefix, ug.group_suffix AS user_suffix, cg.group_prefix AS crawler_prefix, cg.group_suffix AS crawler_suffix
		FROM ' . DB_PREFIX . 'sessions s
		LEFT JOIN ' . DB_PREFIX . 'users u ON s.user_id = u.user_id
		LEFT JOIN ' . DB_PREFIX . 'user_groups ug ON ug.group_id = u.group_id
		LEFT JOIN ' . DB_PREFIX . 'crawlers c ON s.crawler_id = c.crawler_id
		LEFT JOIN ' . DB_PREFIX . 'user_groups cg ON cg.group_id = \'6\'
		WHERE s.session_last_active >= \'' . ($this -> appCore -> time - ($this -> appCore -> conf['session_activity_span'] * 60)) . '\'');
		
		// Set Some Basic Stats
		$this -> tpl_vars['online_total'] = 0;
		$this -> tpl_vars['online_registered'] = 0;
		$this -> tpl_vars['online_hidden'] = 0;
		$this -> tpl_vars['online_guests'] = 0;
		$this -> tpl_vars['online_list'] = '';
		$this -> tpl_vars['online_legend'] = '';
		
		// Go Trought results
		while ( $user_result = $this -> appCore -> db -> fetch_array())
		{
			// Increase Total Counter
			$this -> tpl_vars['online_total'] ++;
						
			// Registered member?
			if ( $user_result[0] > 0)
			{
				// Increase Member Counter
				$this -> tpl_vars[($user_result[3] ? 'online_hidden' : 'online_registered')] ++;
				
				// Hidden dude?
				if ( !$user_result[3] || $this -> appCore -> user_acl['can_see_hidden'])
				{
					// Increase Group Counter
					if ( isset($user_groups[$user_result[2]]))
					{
						$user_groups[$user_result[2]][0] ++;
					}
					
					// Draw User
					$this -> tpl_vars['online_list'] .= ', <a href="' . $this -> appCore -> niceLink( 'act=user&user=' . $user_result[0], $user_result[1]) . '">' . ($this -> appCore -> conf['users_color_always'] ? $user_result[6] : '') . htmlspecialchars( $user_result[1]) . ($this -> appCore -> conf['users_color_always'] ? $user_result[7] : '') . '</a>' . ($user_result[3] ? '*' : '');
					
					// Show hidden legend?
					if ($user_result[3] && $this -> appCore -> user_acl['can_see_hidden'])
					{
						$this -> tpl_vars['hidden_legend'] = 1;
					}
				}
			}
			else
			{
				// Increase Guest Counter
				$this -> tpl_vars['online_guests'] ++;
				
				// Crawler?
				if ( $user_result[4] > 0)
				{
					// Increase Group Counter
					if ( isset($user_groups[6]))
					{
						$user_groups[6][0] ++;
					}
					
					// Draw User
					$this -> tpl_vars['online_list'] .= ', ' . ($this -> appCore -> conf['users_color_always'] ? $user_result[8] : '') . htmlspecialchars( $user_result[5]) . ($this -> appCore -> conf['users_color_always'] ? $user_result[9] : '');
				}
			}
		}
		
		// Free Resources
		$this -> appCore -> db -> freeResult();
		
		// Kill First two characters from online list?
		if ( isset($this -> tpl_vars['online_list'][0]))
		{
			$this -> tpl_vars['online_list'] = substr( $this -> tpl_vars['online_list'], 2);
		}
		
		// Draw Groups Legend?
		if ( !empty( $user_groups))
		{
			// Draw Groups
			foreach ( $user_groups as $group_data)
			{
				$this -> tpl_vars['online_legend'] .= ', <a href="' . $this -> appCore -> niceLink('act=users&search=1&fg=' . $group_data[0], $this -> appCore -> lang['board_nav_users']) . '">' . $group_data[1] . '</a> (' . $this -> appCore -> number( $group_data[0]) . ')';
			}
			
			// Cut first two letters from groups list
			$this -> tpl_vars['online_legend'] = substr( $this -> tpl_vars['online_legend'], 2);
		}
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Users active today/last 24h
	 *
	 */
	
	function drawLastActive(){

		// Today?
		$this -> tpl_vars['last_active_today'] = $this -> appCore -> conf['last_list_index'] == 1;
		
		// Set Stat
		$this -> tpl_vars['last_active_total'] = 0;
		
		// Empty list
		$this -> tpl_vars['last_active_list'] = '';
		
		// Select dudes
		if ($this -> tpl_vars['last_active_today'])
		{
			$this -> appCore -> db -> query('SELECT u.user_id, u.user_name, g.group_prefix, g.group_suffix
			FROM ' . DB_PREFIX . 'users u
			LEFT JOIN ' . DB_PREFIX . 'user_groups g ON u.group_id = g.group_id
			WHERE u.user_last_date >= \'' . ($this -> appCore -> getTimestamp(gmdate('Y'), gmdate('n'), gmdate('j')) + $this -> appCore -> time_zone + ($this -> appCore -> time_dst && date('I') ? 3600 : 0)) . '\'');
		}
		else
		{
			$this -> appCore -> db -> query('SELECT u.user_id, u.user_name, g.group_prefix, g.group_suffix
			FROM ' . DB_PREFIX . 'users u
			LEFT JOIN ' . DB_PREFIX . 'user_groups g ON u.group_id = g.group_id
			WHERE u.user_last_date >= \'' . ($this -> appCore -> time - 86400) . '\'');
		}
		
		// Parse result
		while ($user_result = $this -> appCore -> db -> fetch_array())
		{
			// Increase counter
			$this -> tpl_vars['last_active_total'] ++;
			 
			// Put user on list
			$this -> tpl_vars['last_active_list'] .= ', <a href="' . $this -> appCore -> niceLink('act=user&user=' . $user_result[0], $user_result[1]) . '">' . ($this -> appCore -> conf['users_color_always'] ? $user_result[2] : '') . htmlspecialchars( $user_result[1]) . ($this -> appCore -> conf['users_color_always'] ? $user_result[3] : '') . '</a>';	
		}
		
		// Trim list
		if ( $this -> tpl_vars['last_active_total'] > 0)
		{
			$this -> tpl_vars['last_active_list'] = substr( $this -> tpl_vars['last_active_list'], 2);
		}
		
		// Free result
		$this -> appCore -> db -> freeResult();
	}
	
}